﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NewsMine.DomainObjects;
using NewsMine.Storage;

namespace NewsMine.DataManagers
{
    /// <summary>
    /// Allows to manage the WebSiteInfo,
    /// All services has to contact this class to manage all kinds of the WebsiteInfo changes, or retrivals;
    /// 
    /// </summary>
    public class WebSiteInfoManager
    {
        //todo: tobe implemented.
        //Here we need to make sure all kinds of methods are exposed to create / modify / delete websites including validation.


        public static List<WebSiteInfo> lastRetrivedWebSiteInfoList = null;
        public static DateTime lastWebSiteInfoRetrivedTime= DateTime.MinValue;

        public static List<WebSiteInfo> GetAllDomains(bool forceLatest=false)
        {

            if (lastRetrivedWebSiteInfoList == null || lastWebSiteInfoRetrivedTime < DateTime.UtcNow.AddMinutes(-1))
            {
                var webSiteKVStore = Defaults.WebSiteInfoMasterStore;

                webSiteKVStore.Open();

                int count = webSiteKVStore.GetCount();

                var websiteObjects = webSiteKVStore.Get(0, count + 1);

                var webSites = new List<WebSiteInfo>();

                foreach (var websiteInfoObject in websiteObjects)
                {
                    WebSiteInfo wbSiteInfo = (WebSiteInfo)websiteInfoObject;
                    if (wbSiteInfo.MenuItemconfigs != null && wbSiteInfo.MenuItemconfigs.Count > 0)
                        webSites.Add(wbSiteInfo);
                }
                webSiteKVStore.Close();

                lastRetrivedWebSiteInfoList = webSites;
                lastWebSiteInfoRetrivedTime = DateTime.UtcNow;

                return webSites;
            }
            else
                return lastRetrivedWebSiteInfoList;
        }


        public static List<WebSiteMenu> GetAllMenuItems()
        {
            var webSiteInfoStore = Defaults.WebSiteInfoMasterStore;

            webSiteInfoStore.Open();

            List<object> objs = webSiteInfoStore.Get(0, webSiteInfoStore.GetCount());

            if (objs == null || objs.Count <= 0)
                return null;

            List<WebSiteMenu> menuItems = new List<WebSiteMenu>();
            Dictionary<string, object> sitesForUpdatingBackToDatabase = new Dictionary<string, object>();

            foreach (var obj in objs)
            {
                WebSiteInfo wbSiteInfo = (WebSiteInfo)obj;

                if (wbSiteInfo.MenuItemconfigs == null || wbSiteInfo.MenuItemconfigs.Count <= 0)
                    continue;

                menuItems.AddRange(wbSiteInfo.MenuItemconfigs);
              
            }

            webSiteInfoStore.Close();

            return menuItems;
        }

        public static List<WebSiteInfo> GetWebSitesByDomainEndsWith(string inputDomain)
        {
            var allDomains = GetAllDomains();

            var filteredDomains = (from webInfo in allDomains
                                   where webInfo!=null && !string.IsNullOrEmpty(webInfo.Domain) && webInfo.Domain.ToLower().EndsWith(inputDomain)
                                   select webInfo).ToList();

            return filteredDomains;
        }

        public static List<string> GetDomainNamesByDomainEndsWith(string inputDomain)
        {

            var allDomains = GetAllDomains();

            if (string.IsNullOrEmpty(inputDomain))
                return new List<string>();

            var filteredDomains = (from webInfo in allDomains
                                   where !string.IsNullOrEmpty(webInfo.Domain) && webInfo.Domain.ToLower().EndsWith(inputDomain)
                                   select webInfo.Domain).ToList();

            return filteredDomains;
        }

        public static WebSiteInfo GetDomainByDomainName(string domainName)
        {
            if (string.IsNullOrEmpty(domainName))
                return null;

            return GetAllDomains().Where(d => d.Domain.ToLower() == domainName.ToLower()).FirstOrDefault<WebSiteInfo>();
        }


    }
}
